package sk.hackcraft.multibox.server.net;

import java.util.logging.Level;

import org.json.JSONObject;

import sk.hackcraft.multibox.server.User;
import sk.hackcraft.multibox.server.MultiBox.TokenColisionException;
import sk.nixone.http.ServletExchange;

public class TokenServlet extends JSONServlet {
	public TokenServlet(Server server) {
		super(server);
	}

	@Override
	public JSONObject doRequest(Method method, ServletExchange exchange, JSONObject response) {
		try {
			User newUser = new User(server.box);
			server.box.addUser(newUser);
			response.put("token", newUser.token);
		} catch(TokenColisionException e) {
			Server.logger.log(Level.WARNING, "Wtf? Token collision on generated token?", e);
		}
		return response;
	}
}
